package com.chinamobile.maps.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chinamobile.maps.dao.IUserDAO;
import com.chinamobile.maps.orm.TUser;
import com.chinamobile.maps.util.Struts2Util;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {
	private static final long serialVersionUID = 4120794399269317310L;

	private String id;
	private String username;
	private String password;
	private String emaillist;
	private String message;
	
	private String act;

	@SuppressWarnings("unchecked")
	private List users;
	private IUserDAO userDao = null;


	/**
	 * ע�����û�
	 * @return
	 */
	public String register() {
		if (this.username == null) {
			this.message = "�û�����Ϊ��ֵ��";
			return "error";
		}

		if (this.password == null) {
			this.message = "��¼���벻������Ϊ��ֵ��";
			return "error";
		}

		if (this.emaillist == null) {
			this.message = "�ʼ���ַ����Ϊ��ֵ��";
			return "error";
		}

		TUser user = new TUser();
		user.setUsername(this.username);
		user.setPassword(this.password);
		user.setEmaillist(this.emaillist);
		this.userDao.addUser(user);
		return SUCCESS;
	}

	/**
	 * ��ʾ�û��б�
	 * @return
	 */
	public String list() {
		this.users = this.userDao.getUsers();
		return SUCCESS;
	}

	public String delete() {
		if (id == null || id.length() == 0) return ERROR;
		this.userDao.delUser(Integer.parseInt(this.id));
		return SUCCESS;
	}

	public void test(){
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			Integer carTypeId = Integer.parseInt(request
					.getParameter("carTypeId"));

			out.print("{\"result\":true}");
			out.flush();
			out.close();
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
	public String update() {
		if (this.id == null || id.length() == 0) return ERROR;
		
		if (this.id.equals(this.getAct())) {
			TUser user = new TUser();
			user.setId(Integer.parseInt(this.id));
			user.setUsername(this.username);
			user.setPassword(this.password);
			user.setEmaillist(this.emaillist);
			
			this.userDao.updateUser(user);
			return SUCCESS;
		} else {
			TUser user = this.userDao.getUserByID(Integer.parseInt(this.id));
			this.username = user.getUsername();
			this.password = user.getPassword();
			this.emaillist = user.getEmaillist();
			
			return INPUT;
		}

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmaillist() {
		return emaillist;
	}

	public void setEmaillist(String emaillist) {
		this.emaillist = emaillist;
	}
	
	public String getAct() {
		return act;
	}

	public void setAct(String act) {
		this.act = act;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@SuppressWarnings("unchecked")
	public List getUsers() {
		return users;
	}

	@SuppressWarnings("unchecked")
	public void setUsers(List users) {
		this.users = users;
	}

	public IUserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDAO userDao) {
		this.userDao = userDao;
	}
}